package com.qf.interceptor;

import com.qf.login.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component  // 把拦截器对象放入SpringIOC容器中，就可以使用 依赖注入了
public class TokenInterceptor implements HandlerInterceptor {

    @Autowired
    @Qualifier("jwtTokenServiceImpl")
    private TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //1. 获取请求头中的 token
        String token = request.getHeader("token");

        //2. 判断token是否合法
        boolean flag = tokenService.checkToken(token);
        if (flag){
            return true;
        }else {
            throw new RuntimeException("token非法");

//            return false;
        }

    }

}
